#include "../head/RadarForm.h"
#include "ui_radarform.h"
#include <QPen>
#include <QPainter>
#include <QTime>
#include <QFile>
#include <QtCore/QDir>
#include <QtQuick/QQuickView>
#include <QtQml/QQmlEngine>
#include <QAxObject>


RadarForm::RadarForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RadarForm)
{
    ui->setupUi(this);
    //初始化散点图
//    series1 = new QScatterSeries();              // 创建一个散点数据集对象
//    series1->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
//    series1->setColor(Qt::yellow);
//    series1->setBorderColor(Qt::yellow);
//    series1->setMarkerSize(8);
//    series1->setName("设计曲线");
//    series5 = new QScatterSeries();              // 创建一个散点数据集对象
//    series5->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
//    series5->setMarkerSize(8);
//    series5->setColor(Qt::red);
//    series5->setBorderColor(Qt::red);
//    series5->setName("施工曲线");
//    chart1 = new QChart();
//    axisX1 = new QValueAxis();
//    axisY1 = new QValueAxis();
//    axisX1->setRange(0,160);
//    axisY1->setRange(-20,20);
//    axisY1->setReverse(true);                      //坐标翻转
//    axisX1->setTickCount(36);
//    axisY1->setTickCount(6);
//    axisX1->setLabelFormat("%d");
//    axisY1->setLabelFormat("%0.1f");
//    chart1->addAxis(axisY1, Qt::AlignLeft);                      // 将X轴添加到图表上
//    chart1->addAxis(axisX1, Qt::AlignTop);                    // 将Y轴添加到图表上
//    chart1->legend()->setVisible(true);               // 隐藏图例
// //  m_axisX->setTitleText("X-label");
// //  m_axisY->setTitleText("Y-label");
//    chart1->addSeries(series1);                              // 将曲线对象添加到图表上
//    series1->attachAxis(axisX1);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
//    series1->attachAxis(axisY1);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
//    chart1->addSeries(series5);                              // 将曲线对象添加到图表上
//    series5->attachAxis(axisX1);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
//    series5->attachAxis(axisY1);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
//    chartView1 = new QChartView(chart1);
//     QBrush bsh2 = QBrush(QColor(0, 0, 0), Qt::SolidPattern);
//    chart1->setBackgroundBrush(QColor(0, 0, 0));
//    chart1->axisX()->setLabelsColor(Qt::white);
//    chart1->setTitleBrush(Qt::white);
//    // 设置Y轴颜色
//    chart1->axisY()->setLabelsColor(Qt::white);
//    chart1->legend()->setLabelColor(Qt::white);
//    chart1->legend()->setAlignment(Qt::AlignLeft);
//    chart1->legend()->setFont(QFont("Calibri",12));
//    chart1->layout()->setContentsMargins(0, 0, 0, 0);//设置外边界全部为0
//    chart1->setMargins(QMargins(0, 0, 0, 0));//设置内边界全部为0
//    //chartView1->chart()->setTitle("设计曲线与施工曲线");
//    ui->gridLayoutLine_9->addWidget(chartView1, 1, 1);



//    //初始化散点图
//    series6 = new QScatterSeries();              // 创建一个散点数据集对象
//    series6->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
//    series6->setMarkerSize(8);
//    series6->setBorderColor(Qt::yellow);
//    series6->setName("设计曲线");
//    series6->setColor(Qt::yellow);
//    series7 = new QScatterSeries();              // 创建一个散点数据集对象
//    series7->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
//    series7->setMarkerSize(8);
//    series7->setBorderColor(Qt::red);
//    series7->setName("施工曲线");
//    series7->setColor(Qt::red);
//    chart4 = new QChart();
//    axisX2 = new QValueAxis();
//    axisY2 = new QValueAxis();
////    axisX2->setMin(0);
////    axisY2->setMin(0);
//    axisX2->setRange(0,160);
//    axisY2->setRange(0,20);

//    axisY2->setReverse(true);                      //坐标翻转
//    axisX2->setTickCount(36);
//    axisY2->setTickCount(6);
//    axisX2->setLabelFormat("%d");
//    axisY2->setLabelFormat("%0.1f");
//    chart4->addAxis(axisY2, Qt::AlignLeft);                      // 将X轴添加到图表上
//    chart4->addAxis(axisX2, Qt::AlignTop);                    // 将Y轴添加到图表上
//    chart4->legend()->setVisible(true);               // 隐藏图例
// //  m_axisX->setTitleText("X-label");
// //  m_axisY->setTitleText("Y-label");
//    chart4->addSeries(series6);                              // 将曲线对象添加到图表上
//    series6->attachAxis(axisX2);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
//    series6->attachAxis(axisY2);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
//    chart4->addSeries(series7);                              // 将曲线对象添加到图表上
//    series7->attachAxis(axisX2);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
//    series7->attachAxis(axisY2);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
//    chart4->setBackgroundBrush(QColor(0, 0, 0));
//    chart4->axisX()->setLabelsColor(Qt::white);
//    // 设置Y轴颜色
//    chart4->axisY()->setLabelsColor(Qt::white);
//    chart4->setTitleBrush(Qt::white);
//    chart4->legend()->setLabelColor(Qt::white);
//    chart4->legend()->setAlignment(Qt::AlignLeft);
//    chart4->legend()->setFont(QFont("Calibri",12));
//    chart4->layout()->setContentsMargins(0, 0, 0, 0);//设置外边界全部为0
//    chart4->setMargins(QMargins(0, 0, 0, 0));//设置内边界全部为0
//    chartView2 = new QChartView(chart4);

//    //chartView2->chart()->setTitle("设计曲线与施工曲线");
//    ui->gridLayoutLine_10->addWidget(chartView2, 1, 1);
   // connect(mutiSignalDecode, &MutiSignalDecode::sendDecCode2WorkPlat, this,&RadarForm::staticAndDynamicData);
    connect(mutiSignalDecode, &MutiSignalDecode::sendDeCodeData, this,&RadarForm::acceptDecodeData);
    //ui->verticalLayout_12->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
   // read_Excel();
}

void RadarForm::clearPlainText(){
    ui->plainTextEdit->clear();
     ui->plainTextEdit_2->clear();
    decodeStr[0].clear();
    decodeStr[1].clear();
//    ui->plainTextEdit_2->clear();
}

void RadarForm::setPlainText(int hzType){
    ui->plainTextEdit->clear();
    for(int i= 0 ;i < decodeStr[hzType].size();i++ ){
        ui->plainTextEdit->insertPlainText(decodeStr[hzType][i]);
    }
}

void RadarForm::acceptDecodeData(QString decData,int hzType){
   if(hzType == 1){
        if("DATA" == decData){
            ui->plainTextEdit->moveCursor (QTextCursor::End);
            ui->plainTextEdit->insertPlainText("\r\n");
            ui->plainTextEdit->moveCursor (QTextCursor::End);
        }else{
            ui->plainTextEdit->moveCursor (QTextCursor::End);
            ui->plainTextEdit->insertPlainText (decData + " ");
            ui->plainTextEdit->moveCursor (QTextCursor::End);
        }
    }else{
        if("DATA" == decData){
            ui->plainTextEdit_2->moveCursor (QTextCursor::End);
            ui->plainTextEdit_2->insertPlainText("\r\n");
            ui->plainTextEdit_2->moveCursor (QTextCursor::End);
        }else{
            ui->plainTextEdit_2->moveCursor (QTextCursor::End);
            ui->plainTextEdit_2->insertPlainText (decData + " ");
            ui->plainTextEdit_2->moveCursor (QTextCursor::End);
        }
    }

}

void RadarForm::read_Excel(){

    //读取钻杆信息
//    QString fileName = QApplication::applicationDirPath() + "/conf/InitParam.ini";

//    QFile file1;
//    file1.setFileName(fileName);
//    QStringList  user_data;
//    double ZGCD = mutiSignalDecode->sys_zgcd[1];


//    ui->zgcd_3->setText(QString::number(ZGCD,'f',1));
//    ui->zgcd_3->setReadOnly(true);

//    //读取设计曲线信息
//    fileName = QApplication::applicationDirPath() + "/conf/shejiquxian.txt";
//    QFile file(fileName);
//    if(!file.open(QIODevice::ReadOnly)){
//        qDebug()<<"打开失败";
//    }

//    QTextStream in(&file);


//    while(!in.atEnd()){
//        QString line = in.readLine();
//        user_data = line.split("|");

//        x_1 = x_1 + ZGCD * cos(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
//        y_1 = y_1 + ZGCD * sin(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
//        z_1 = z_1 + ZGCD * cos(user_data[3].toFloat()*PI/180);

//        series1->append(x_1,y_1);//侧视图
//        series6->append(x_1,z_1);//俯视图
//    }
//    file.close();


//    //读取施工曲线信息
//    fileName = QApplication::applicationDirPath() + "/conf/shigongquxian.txt";
//    QFile file2(fileName);
//    if(!file2.open(QIODevice::ReadOnly)){
//        qDebug()<<"打开失败";
//    }

//    QTextStream in2(&file2);


//    while(!in2.atEnd()){
//        QString line = in2.readLine();
//        user_data = line.split("|");
//      //  figureDrillPosition(user_data[2].toFloat(),user_data[3].toFloat(),ZGCD);
//        x_2 = x_2 + ZGCD * cos(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
//        y_2 = y_2 + ZGCD * sin(user_data[2].toFloat()*PI/180) * sin(user_data[3].toFloat()*PI/180);
//        z_2 = z_2 + ZGCD * cos(user_data[3].toFloat()*PI/180);

//        series5->append(x_2,y_2);
//        series7->append(x_2,z_2);

//    }
//    file2.close();

}


void RadarForm::figureDrillPosition (double courseAngle, double pitchAngle, double drillLength) { //courseAngle 航向角， pitchAngle 俯仰角，drillLength 钻杆长度
    x_2 = x_2 + drillLength * cos(courseAngle*PI/180) * sin(pitchAngle*PI/180);
    y_2 = y_2 + drillLength * sin(courseAngle*PI/180) * sin(pitchAngle*PI/180);
    z_2 = z_2 + drillLength * cos(pitchAngle*PI/180);
    series5->append(x_2,y_2);
    series7->append(x_2,z_2);
}

void RadarForm::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
//    QPainter painter(this);
//    QPen pen;
//    //int width = this->geometry().width();
//    int height = this->geometry().height();

//    painter.setRenderHint(QPainter::Antialiasing);
//    painter.setPen(QPen(Qt::white));

//    //网格改成坐标系
//    int x=140,y=20,offsetY=0,i=0; //X

//    offsetY = (height/2-x)/2-90; //窗口高度减菜单栏-状态栏-工具栏高度
//    if(offsetY<=10){
//        offsetY =10;
//    }
//    QPoint center(x+2*y+4,x+y+4+offsetY);
//    QRectF rectangle(2*y+4,(x-y*4-12)/2+offsetY, x*2, x*2);    //给定的矩形，我们会直接用正方形来绘制正圆弧

//    int startAngle = 90 * 16;    //起始角，以3点方向为0°，逆时针为整。
//    int spanAngle = 180 * 16;    //偏移角，就是起始角和终止角的差值。绘制整个圆就输入360 * 16
//    painter.drawArc(rectangle, startAngle, spanAngle);    //调用绘图命令

//    painter.drawEllipse(center,x-y,x-y);
//    painter.drawEllipse(center,x-2*y,x-2*y);
//    painter.drawEllipse(center,5,5);

//    /** 画外圈刻度值，将坐标系移动到圆心 **/
//    painter.translate(x+2*y+4,x+y+4+offsetY);

//    painter.setFont(QFont("Calibri",10));
//    QFont font = painter.font();
//    font.setPointSize(12);
//    font.setWeight(60);
//    painter.setFont(font);

//    //绘制内圈
//    for(i=1;i<=72;i++){
//        painter.rotate(5);
//        if(i % 2 == 1){
//             painter.drawLine(0,-(x-2*y),0,-(x-2*y-4));
//        }
//    }
//    //    for(i=1;i<=36;i++){
//    //        painter.drawText(-(x-2*y-50),-(x-2*y),20,10,Qt::AlignCenter,QString::number(i*10));//位置和内容
//    //    }

//    for(i=1;i<=36;i++){
//        painter.rotate(10);
//        painter.drawLine(0,-(x-2*y),0,-(x-2*y-8));

//    }

//    for(i=1;i<=4;i++){
//        painter.rotate(90);
//        painter.drawLine(0,-(x-2*y),0,-(x-2*y-y/2));
//    }

//    for(i =0;i<12;i++){
//        painter.drawText(-(x-2*y-89),-(x-2*y-12),23,12,Qt::AlignCenter,QString::number(i*30));//位置和内容
//        painter.rotate(30);
//    }


//    //最外圈
//    for(i=1;i<72;i++){
//        painter.rotate(5);
//        if(i > 36 && i % 2 == 1){
//            painter.drawLine(0,-x,0,-(x-6));
//        }
//    }
//    painter.rotate(5);

//    for(i=1;i<36;i++){
//        painter.rotate(10);
//        if(i> 18){
//            painter.drawLine(0,-x,0,-(x-10));
//        }
//    }
//    painter.rotate(10);

//    for(i=1;i<=4;i++){
//        painter.rotate(90);

//        if(i == 2 || i == 4){
//            painter.drawLine(0,-x-8,0,-(x-y));
//        }
//    }

//    QString number1[13] = {"","","","","","","-90","-60","-30","0","30","60","90"};
//    for(i =0;i<13;i++){
//        painter.drawText(-(x-129),-(x-y+39),23,12,Qt::AlignCenter,number1[i]);//位置和内容
//        painter.rotate(30);
//    }

//    painter.rotate(-30);

//    for(i=0;i<=3600;i++){
//        painter.setPen(QPen(Qt::gray,4));
//        painter.rotate(0.1);

//        int n1 = angle[0]*10,n2=angle[1]*10,n3=(angle[2]+270)*10;

//        if(i==n2){ //工具面角

//            painter.setPen(QPen(QColor("#ff5500"),3));
//            painter.drawLine(0,-(x-3*y-10),0,0);
//        }

//        if(i == n1){//方位角

//            painter.setPen(QPen(Qt::yellow,3));
//            painter.drawLine(0,-(x-y-1),0,-(x-2*y+1));
//        }

//        if(i==n3){ //倾角

//            painter.setPen(QPen(QColor("#0000ff"),3));
//            painter.drawLine(0,-x,0,-(x-y+1));
//            //painter.drawLine(0,-x,0,-(x-y));
//        }
//    }

}

/****AA AA 01 20 03 9F 0A 2C 26 13 1A 2C 00 88
 * 20 03 倾角 INC 低八位与上高八位除以10 ，减去90； (5位 4位)
 * 9F 0A 方位角 AZi 低八位与上高八位除以10     (7位6位)
 * 2C 26 重力和 GT 低八位与上高八除以10000 (9位，8位)
 * 13 工具面角 TF 直接乘2 (10位)
 * 1A 温度 T
 * 2C 场强  MAG
 * 00 ? 磁偏角
 * 88 电压 BTT 直接除以10
 * **********************************************/

/****AA AA 01 04 4C 0B 80 6A A0 E9 28 1B 18 04 4B
 * 04 4C 倾角 INC 低八位与上高八位除以10 ，减去90； (5位 4位)
 * 0B 80 方位角 AZi 低八位与上高八位除以10     (7位6位)
 * 6A A0 重力和 GT 低八位与上高八除以10000 (9位，8位)
 * E9 温度 T
 * 28 场强  MAG
 * 1B DIP
 * 18 电压 BTT 直接除以10
 * 04 4B NBINC
 * **********************************************/
void RadarForm::staticAndDynamicData(QByteArray decData){
    double data1=0;

//    if(decData.size() == DEC_LONG_LENGTH){ //静态数据
//        data1 = ((double)((uint8_t)decData[4]*256 + (uint8_t)decData[3])/10)-90; //倾角
//        angle[2] = data1;
//        ui->label_INC_3->setText(QString::number(data1,'f',1));
//        ui->label_Inc->setText("倾角："+QString::number(data1,'f',1)+"°");
//        ui->qj_3->setText(QString::number(data1,'f',1));

//        data1 = (double)((uint8_t)decData[6]*256+(uint8_t)decData[5])/10; //方位角
//        angle[0] = data1;
//        ui->label_AZI_3->setText(QString::number(data1,'f',1));
//        ui->label_Azi->setText("方位角："+QString::number(data1,'f',1)+"°");
//        ui->fwj_3->setText(QString::number(data1,'f',1));

////        data1 = (double)((uint8_t)decData[9])*2; //工具面角
////        angle[1] = data1;
////        ui->label_TF_3->setText(QString::number(data1,'f',1));
////        ui->label_Tf->setText("工具面："+QString::number(data1,'f',1)+"°");

//        data1 = (double)((uint8_t)decData[8]*256+(uint8_t)decData[7])/10000; //重力和
//        ui->label_GT_3->setText(QString::number(data1,'f',4));

//        ui->label_TEMP_3->setText(QString::number((uint8_t)decData[9],'f',1)); //温度

//        ui->label_BT_3->setText(QString::number((uint8_t)decData[10],'f',1)); //场强

//        ui->label_DIP_3->setText(QString::number((uint8_t)decData[11],'f',1)); //磁偏角


//        data1 = (double)((uint8_t)decData[12])/10; //电压
//        ui->label_BATV_3->setText(QString::number(data1,'f',1));

//        if(data1<=6.0 && dyInfo == 0){
//            QMessageBox::warning(this,"电压低","电压低于6V！");
//            dyInfo = 1;
//        }else if(data1<=5.5){
//            QMessageBox::critical(this,"电压低","电源即将耗完！");
//        }

//    }else if(decData.size() == DEC_SHORT_LENGTH){ //动态数据
//        data1 = (double)((uint8_t)decData[3])*2; //工具面角
//        angle[1] = data1;
//        ui->label_TF_3->setText(QString::number(data1,'f',1));
//        ui->label_Tf->setText("工具面："+QString::number(data1,'f',1)+"°");
//    }

//    this->update();
}

RadarForm::~RadarForm()
{
    delete ui;
}

